﻿@using Gurux.DLMS.AMI.Shared.DTOs

@if (Target?.Attributes != null && Target.Template != null && Target.Attributes.Any())
{
    <CascadingValue Value="this">
        <TabControl>
            <TabTextTemplate>
                <span class=@context.Icon aria-hidden="true"></span>
                @context.Text
            </TabTextTemplate>
            <ChildContent>
                <TabPage Text="General">
                    <AttributeTile Index="0" Name="@Properties.Resources.Description" Text="@Target.Template.Name"></AttributeTile>
                    <AttributeTile Index="1" Name="@Properties.Resources.LogicalName" Text="@Target.Template.LogicalName"></AttributeTile>
                    <AttributeTile Index="2" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="3" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="4" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="7" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="8" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="9" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="10" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="11" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="12" Attributes="@Target.Attributes"></AttributeTile>
                    <AttributeTile Index="13" Attributes="@Target.Attributes"></AttributeTile>
                    @ChildContent
                </TabPage>
                <TabPage Text="Unit charge active">
                    <ChargeUnit @ref="_active" Target="@Target" Index="5"></ChargeUnit>
                </TabPage>
                <TabPage Text="Unit charge passive">
                    <ChargeUnit Target="@Target" Index="6"></ChargeUnit>
                </TabPage>
            </ChildContent>
        </TabControl>
    </CascadingValue>
}

@code {

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    [CascadingParameter]
    private ObjectView? Parent { get; set; }

    /// <summary>
    /// Target COSEM object.
    ///</summary>
    [Parameter]
    public GXObject? Target { get; set; }

    private void Activate()
    {
        Parent?.OnAction(2, (sbyte)0);
    }

    private void Collect()
    {
        Parent?.OnAction(3, (sbyte)0);
    }

    private ChargeUnit? _active;
    private void UpdateTotal()
    {
        if (_active != null)
        {
            Parent?.OnAction(4, _active.PriceScale);
        }
    }

    private void SetTotal()
    {
        if (_active != null)
        {
            Parent?.OnAction(5, _active.PriceScale);
        }
    }

    protected override void OnInitialized()
    {
        if (Parent == null)
        {
            throw new ArgumentNullException(nameof(Parent), "View must exist within a Object view");
        }
        Parent.MenuItems.Add(MenuItem.Create("Activate", "",
               Collect,
               !Parent.CanInvokeAction(2)));
        Parent.MenuItems.Add(MenuItem.Create("Collect", "",
               Collect,
               !Parent.CanInvokeAction(3)));
        Parent.MenuItems.Add(MenuItem.Create("Update total", "",
       UpdateTotal,
       !Parent.CanInvokeAction(4)));
        Parent.MenuItems.Add(MenuItem.Create("Set total", "",
               SetTotal,
               !Parent.CanInvokeAction(5)));
        Parent.Update();

    }
}